package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.statistics.RoleStatistics;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.logic.bussiness.statistics.RoleStatisticsBusiness;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import com.yanqu.road.web.mgr.UserIdMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/queryTimeServerRoleCreate")
public class QueryTimeServerRoleCreate extends BaseJsonHttpServlet {

	private static Logger logger = LogManager.getLogger(QueryTimeServerRoleCreate.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("request queryTimeServerRoleCreate");
		HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
		JSONObject paramObj = httpJsonWebParam.getParamObj();
        long startTime;
        long endTime;
        int type = 1;
        JSONObject resultObj = new JSONObject();
        try{
            startTime = paramObj.getLongValue("startTime");
            endTime = paramObj.getLongValue("endTime");
			if(paramObj.containsKey("type")){
				type = paramObj.getIntValue("type");
			}
        }catch (Exception e){
            logger.error("queryTimeServerRoleCreate argument error:" + e);
            sendMessage(response, resultObj.toString());
            return;
        }
        if(startTime > endTime){
            logger.error("queryTimeServerRoleCreate startTime endTime error,startTime:{},endTime:{}" , startTime, endTime);
            sendMessage(response, resultObj.toString());
            return;
        }
        long startHoursTimeStamp = DateHelper.getHoursTimeStamp(startTime);
        long endHoursTimeStamp = DateHelper.getHoursTimeStamp(endTime);
		List<RoleStatistics> roleStatisticsList = RoleStatisticsBusiness.getRoleStatisticsList(startHoursTimeStamp, endHoursTimeStamp);
		RoleStatistics nowRoleStatistics = UserIdMgr.getCurRoleStatistics();
		Map<String, JSONObject> dateObjMap = new HashMap<>();
		for(RoleStatistics roleStatistics : roleStatisticsList){
			long roleCreateCount = roleStatistics.getCreateRoleCount();
			long roleCreateCount2 = roleStatistics.getCreateRoleCount2();
			long registerCount = roleStatistics.getRegisterCount();
			if(null != nowRoleStatistics && roleStatistics.getHoursTimeStamp() == nowRoleStatistics.getHoursTimeStamp()){
				roleCreateCount = nowRoleStatistics.getCreateRoleCount();
				roleCreateCount2 = nowRoleStatistics.getCreateRoleCount2();
				registerCount = nowRoleStatistics.getRegisterCount();
			}
			String date = DateHelper.getYMDBARString(roleStatistics.getHoursTimeStamp());
			int hours = DateHelper.getTimeStampHours(roleStatistics.getHoursTimeStamp());
			if(!dateObjMap.containsKey(date)){
				dateObjMap.put(date, new JSONObject());
			}
			JSONObject dateObj = dateObjMap.get(date);
			dateObj.put("" + hours, roleCreateCount);
			if(type == 2){
				dateObj.put("" + hours, roleCreateCount2);
			}else if(type == 3){
				dateObj.put("" + hours, registerCount);
			}
		}
		JSONObject serverObj = new JSONObject();
		for(Map.Entry<String, JSONObject> dataEntry : dateObjMap.entrySet()){
			serverObj.put(dataEntry.getKey(), dataEntry.getValue());
		}
		resultObj.put("" + WebServer.getInstance().getServerId(), serverObj);

		sendMessage(response, resultObj.toString());
	}
}
